<template>
  <div class="height_width-full">
    <div class="breadcrumb">
      <div class="breadcrumb-icon-box">
        <i class="el-icon-location-outline"></i>
      </div>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path : '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>人员管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-divider></el-divider>
    <div>
      <div class="person-search">
        <el-row :gutter="10">
          <el-col :span="3">
            <el-select v-model="search.depart" placeholder="所属部门" clearable>
              <i slot="prefix" class="el-input__icon el-icon-search"></i>
              <el-option :label="item.title" :value="item.title" v-for="item in departList" :key="item.id"></el-option>
            </el-select>
          </el-col>
          <el-col :span="3">
            <el-select v-model="search.taskSoftType" placeholder="软件类型" clearable>
              <i slot="prefix" class="el-input__icon el-icon-search"></i>
              <el-option :label="item.name" :value="item.value" v-for="(item,index) in taskSoftTypeList"
                         :key="index"></el-option>
            </el-select>
          </el-col>
          <el-col :span="3">
            <el-input clearable
                      placeholder="姓名"
                      v-model="search.username">
              <i slot="prefix" class="el-input__icon el-icon-search"></i>
            </el-input>
          </el-col>
          <el-col :span="5">
            <el-button type="success" @click="searchTable">搜索</el-button>
            <el-button type="primary" @click="goto('/asset-edit-person' , 'EditPerson' , {type : 1, data : null})">添加
            </el-button>
          </el-col>
        </el-row>
      </div>
      <el-table
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column type="expand"
                         fixed
                         prop="userId">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="用户名">
                <span>{{ props.row.username }}</span>
              </el-form-item>
              <el-form-item label="昵称">
                <span>{{ props.row.nickname }}</span>
              </el-form-item>
              <el-form-item label="性别">
                <span>{{ props.row.gender == 1 ? '男' : '女' }}</span>
              </el-form-item>
              <el-form-item label="生日">
                <span>{{ new Date(props.row.birthday).format("yyyy-MM-dd") }}</span>
              </el-form-item>
              <el-form-item label="电话">
                <span>{{ props.row.phone }}</span>
              </el-form-item>
              <el-form-item label="部门">
                <span>{{ props.row.depart }}</span>
              </el-form-item>
              <el-form-item label="职位">
                <span>{{ props.row.post }}</span>
              </el-form-item>
              <el-form-item label="软件类型">
                <span>{{ props.row.taskSoftType == 1 ? 'BS软件' : 'CS软件' }}</span>
              </el-form-item>
              <el-form-item label="用户类型">
                <span>{{ props.row.userType == 1 ? '系统内置' : '外部引入' }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column
          prop="username"
          label="姓名"
          width="120">
        </el-table-column>
        <el-table-column
          prop="nickname"
          label="昵称"
          width="120">
        </el-table-column>
        <el-table-column
          prop="gender"
          label="性别"
          width="90">
          <template slot-scope="scope">
            {{ scope.row.gender == 1 ? "男" : "女" }}
          </template>
        </el-table-column>
        <el-table-column
          prop="depart"
          label="部门"
          width="150">
        </el-table-column>
        <el-table-column
          prop="post"
          label="职位"
          width="120">
        </el-table-column>
        <el-table-column
          prop="taskSoftType"
          label="软件类型"
          width="90">
          <template slot-scope="scope">
            {{ scope.row.taskSoftType == 1 ? "BS软件" : "CS软件" }}
          </template>
        </el-table-column>
        <el-table-column
          prop="phone"
          label="电话"
          width="200">
        </el-table-column>
        <el-table-column
          prop="birthday"
          label="生日"
          width="200">
          <template slot-scope="scope">
            {{ new Date(scope.row.birthday).format("yyyy-MM-dd") }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="120">
          <template slot-scope="scope">
            <el-button type="primary" size="small" icon="el-icon-edit"
                       @click="goto('/asset-edit-person' , 'EditPerson' , {type : 0, data : scope.row})"></el-button>
            <el-button type="danger" size="small" icon="el-icon-delete"
                       @click="delPerson(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page-box">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.currentPage"
          :page-sizes="page.pageSizes"
          :page-size="page.pageSize"
          layout=" prev, pager, next, jumper , sizes, total"
          :total="page.totalCount">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Person",
  data() {
    return {
      search: {
        depart: "",
        taskSoftType: "",
        username: ""
      },
      page: {
        currentPage: 1,
        pageSize: 10,
        pageSizes: [10, 50, 70, 100],
        totalCount: 0
      },
      departList: [],
      tableData: [],
      taskSoftTypeList: [
        {
          name: "BS软件",
          value: 1,
        }, {
          name: "CS软件",
          value: 2
        }
      ]
    }
  },
  created() {
    this.getTableData(this.page.currentPage, this.page.pageSize);
    this.getDepartList();
  },
  methods: {
    searchTable() {
      this.getTableData(1, 10);
    },
    getDepartList() {
      this.$get(this.$api.API_DEPART_LIST, {
        pageNum: 1,
        pageSize: 100
      }).then(res => {
        if (res.data.code == 200) {
          this.departList = res.data.data.list;
        }
      })
    },
    getTableData(pageNum, pageSize) {
      this.$get(this.$api.API_USER_LIST, {
        depart: this.search.depart,
        taskSoftType: this.search.taskSoftType,
        username: this.search.username,
        pageNum: pageNum,
        pageSize: pageSize,
        id: this.$store.getters.getUser.id
      }).then(res => {
        console.log(res)
        if (res.data.code == 200) {
          this.tableData = res.data.data.list;
          this.page.currentPage = res.data.data.pageNum;
          this.page.pageSize = res.data.data.pageSize;
          this.page.totalCount = res.data.data.total;
        }
      })
    },
    handleClick(row) {
      console.log(row);
    },
    handleSizeChange(data) {
      this.page.pageSize = data;
      this.getTableData(this.page.currentPage, data);
    },
    handleCurrentChange(data) {
      this.getTableData(data, this.page.pageSize);
    },
    goto(path, name, data) {
      this.$router.push({
        path, name
      })
      this.Storage.Session.set("personData", data);
    },
    delPerson(id) {
      console.log(id);
      this.$confirm('确定永久删除该人员吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$delete(this.$api.API_USER_DELETE + `/${id}`).then(res => {
          console.log(res);
          if (res.data.code == 200) {
            this.$notify.success(res.data.message);
            this.getTableData(this.page.currentPage, this.page.pageSize);
          } else {
            this.$notify.error(res.data.message);
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消删除'
        })
      })
    }
  },
}
</script>

<style scoped>
.page-box {
  margin-top: 10px;
}

.person-search {
  margin-bottom: 20px;
}

.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
